package com.bocai.freeman.view.custom;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Shader;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ClipDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.LayerDrawable;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.StateListDrawable;
import android.graphics.drawable.shapes.RoundRectShape;
import android.graphics.drawable.shapes.Shape;
import android.util.AttributeSet;
import android.view.Gravity;
import android.widget.RatingBar;

import com.bocai.freeman.R;

/**
 * Created by win7 on 2016/2/19.
 */
public class MRatingBar extends RatingBar {
    private int starWidth;

    private int spacing;

    private float rating;

    public MRatingBar(Context context, AttributeSet attrs) {
        super(context, attrs);

        //setMax(1000);

        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.MRatingBar);

        spacing = a.getDimensionPixelSize(R.styleable.MRatingBar_spacing, 0);

        Drawable drawable = a.getDrawable(R.styleable.MRatingBar_mProgressDrawable);
        if (drawable != null) {
            drawable = tileify(drawable, false);
            // Calling this method can set mMaxHeight, make sure the corresponding
            // XML attribute for mMaxHeight is read after calling this method
            setProgressDrawable(drawable);
        }

        setRating(rating);
    }

    private Drawable tileify(Drawable drawable, boolean clip) {
        if (drawable instanceof LayerDrawable) {
            LayerDrawable background = (LayerDrawable) drawable;
            final int N = background.getNumberOfLayers();
            Drawable[] outDrawables = new Drawable[N];

            for (int i = 0; i < N; i++) {
                int id = background.getId(i);
                outDrawables[i] = tileify(background.getDrawable(i),
                        (id == android.R.id.progress || id == android.R.id.secondaryProgress));
            }

            LayerDrawable newBg = new LayerDrawable(outDrawables);

            for (int i = 0; i < N; i++) {
                newBg.setId(i, background.getId(i));
            }

            return newBg;

        } else if (drawable instanceof StateListDrawable) {
//            StateListDrawable in = (StateListDrawable) drawable;
//            StateListDrawable out = new StateListDrawable();
//            int numStates = in.getStateCount();
//            for (int i = 0; i < numStates; i++) {
//                out.addState(in.getStateSet(i), tileify(in.getStateDrawable(i), clip));
//            }
//            return out;

        } else if (drawable instanceof BitmapDrawable) {
            final Bitmap tileBitmap = ((BitmapDrawable) drawable).getBitmap();

            if (starWidth == 0) {
                starWidth = tileBitmap.getWidth();
            }

            final Bitmap bitmap = Bitmap.createBitmap(tileBitmap.getWidth() + spacing, tileBitmap.getHeight(), Bitmap.Config.ARGB_8888);
            Canvas canvas = new Canvas(bitmap);

            canvas.drawBitmap(tileBitmap, spacing / 2, 0, null);

            final ShapeDrawable shapeDrawable = new ShapeDrawable(getDrawableShape());

            final BitmapShader bitmapShader = new BitmapShader(bitmap,
                    Shader.TileMode.REPEAT, Shader.TileMode.CLAMP);
            shapeDrawable.getPaint().setShader(bitmapShader);

            return (clip) ? new ClipDrawable(shapeDrawable, Gravity.LEFT,
                    ClipDrawable.HORIZONTAL) : shapeDrawable;
        }

        return drawable;
    }

    @Override
    public void setRating(float rating) {

        if (starWidth != 0) {
//        	int rate = (int) rating;
//    		float progress = ((rating - rate) * starWidth + rate * (starWidth + spacing)) * getMax()
//    				/ (starWidth * getNumStars() + spacing * (getNumStars() - 1));
//    		setProgress(Math.round(progress));
            super.setRating(rating);
        }

        this.rating = rating;
    }

    Shape getDrawableShape() {
        final float[] roundedCorners = new float[] { 5, 5, 5, 5, 5, 5, 5, 5 };
        return new RoundRectShape(roundedCorners, null, null);
    }

    @Override
    protected synchronized void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        if (starWidth != 0) {
            // TODO: Once ProgressBar's TODOs are gone, this can be done more
            // cleanly than mSampleTile
            //final int width = starWidth * getNumStars() + spacing * (getNumStars() - 1);
            final int width = (starWidth + spacing) * getNumStars();
            setMeasuredDimension(resolveSize(width, widthMeasureSpec), getMeasuredHeight());
        }
    }
}
